package views.ope;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Turma;
import models.cad.Usuario;
import models.ope.Valor;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class OpeLVlrGUI extends GUIInternalFrame implements GUICadInterface{
	public JFormattedTextField tfIs;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JFormattedTextField tfAno;
	public JButton btNTur;
	public JFormattedTextField tfCodTur;
	public JFormattedTextField tfTVlr;
	public JLabel lbTur2;
	public JLabel lbTur1;
	public JLabel lbTur;
	public JLabel lbAtv;
	public JLabel lbAtv1;
	public JLabel lbAtv2;
	public JFormattedTextField tfCodAtv;
	public JButton btNAtv;
	public JFormattedTextField tfAVlr;
	public JTabbedPane tpTipo;
	public JPanel tpPnTur;
	public JPanel tpPnAtv;
	
	public OpeLVlrGUI() {
		setTitle("Lan\u00E7a Valores");
		setSize(new Dimension(330, 330));
		try {
			
			JLabel lblAno = new JLabel("Ano:");
			lblAno.setBounds(26, 58, 34, 14);
			getContentPane().add(lblAno);
			
			tfAno = new JFormattedTextField(new MaskFormatter("####"));
			tfAno.setColumns(4);
			tfAno.setBounds(82, 55, 30, 20);
			getContentPane().add(tfAno);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(26, 252, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel_1.add(btListar);
			
			tpTipo = new JTabbedPane(JTabbedPane.TOP);
			tpTipo.setBounds(26, 83, 268, 158);
			getContentPane().add(tpTipo);
			
			tpPnTur = new JPanel();
			tpTipo.addTab("Por Turma", null, tpPnTur, null);
			tpPnTur.setLayout(null);
			
			JLabel label = new JLabel("Turma:");
			label.setBounds(10, 15, 57, 14);
			tpPnTur.add(label);
			
			tfCodTur = new JFormattedTextField(new MaskFormatter("###"));
			tfCodTur.setColumns(3);
			tfCodTur.setBounds(50, 12, 30, 20);
			tpPnTur.add(tfCodTur);
			
			btNTur = new JButton("N");
			btNTur.setBounds(82, 11, 23, 23);
			tpPnTur.add(btNTur);
			
			lbTur = new JLabel(".");
			lbTur.setBounds(112, 15, 130, 14);
			tpPnTur.add(lbTur);
			
			lbTur1 = new JLabel(".");
			lbTur1.setBounds(112, 40, 130, 14);
			tpPnTur.add(lbTur1);
			
			lbTur2 = new JLabel(".");
			lbTur2.setBounds(112, 65, 130, 14);
			tpPnTur.add(lbTur2);
			
			JLabel label_4 = new JLabel("Valor:");
			label_4.setBounds(10, 96, 40, 14);
			tpPnTur.add(label_4);
			
			tfTVlr = new JFormattedTextField(new MaskFormatter("R$ ###,##"));
			tfTVlr.setColumns(5);
			tfTVlr.setBounds(50, 93, 65, 20);
			tpPnTur.add(tfTVlr);
			
			tpPnAtv = new JPanel();
			tpTipo.addTab("Por Atividade", null, tpPnAtv, null);
			tpPnAtv.setLayout(null);
			
			JLabel lblAtividade = new JLabel("Atividade:");
			lblAtividade.setBounds(10, 15, 57, 14);
			tpPnAtv.add(lblAtividade);

			tfIs = new JFormattedTextField(new MaskFormatter("###"));
			tfIs.setColumns(3);
			tfIs.setBounds(82, 27, 30, 20);
			getContentPane().add(tfIs);
			
			tfCodAtv = new JFormattedTextField(new MaskFormatter("###"));
			tfCodAtv.setColumns(3);
			tfCodAtv.setBounds(66, 12, 30, 20);
			tpPnAtv.add(tfCodAtv);
			
			btNAtv = new JButton("N");
			btNAtv.setBounds(98, 11, 23, 23);
			tpPnAtv.add(btNAtv);
			
			lbAtv = new JLabel(".");
			lbAtv.setBounds(128, 15, 120, 14);
			tpPnAtv.add(lbAtv);
			
			JLabel label_7 = new JLabel("Valor:");
			label_7.setBounds(10, 96, 57, 14);
			tpPnAtv.add(label_7);
			
			tfAVlr = new JFormattedTextField(new MaskFormatter("R$ ###,##"));
			tfAVlr.setColumns(5);
			tfAVlr.setBounds(66, 93, 65, 20);
			tpPnAtv.add(tfAVlr);
			
			lbAtv1 = new JLabel(".");
			lbAtv1.setBounds(128, 40, 120, 14);
			tpPnAtv.add(lbAtv1);
			
			lbAtv2 = new JLabel(".");
			lbAtv2.setBounds(128, 65, 120, 14);
			tpPnAtv.add(lbAtv2);
			
			JLabel label_1 = new JLabel("Insc.:");
			label_1.setBounds(26, 30, 34, 14);
			getContentPane().add(label_1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfIs.addFocusListener(fl);
        tfAno.addFocusListener(fl);
        tfCodTur.addFocusListener(fl);
        tfCodAtv.addFocusListener(fl);
        tfAVlr.addFocusListener(fl);
        tfTVlr.addFocusListener(fl);
        btNTur.addActionListener(ac);
        btNAtv.addActionListener(ac);
        btNTur.addFocusListener(fl);
        btNAtv.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);
	}

	@Override
	public void limpar() {
		tfIs.setText("");
		tfAno.setText("");
		tfCodTur.setText("");
		tfTVlr.setText("");
		lbAtv.setText("");
		lbAtv1.setText("");
		lbAtv2.setText("");
		lbTur.setText("");
		lbTur1.setText("");
		lbTur2.setText("");
		btExcluir.setEnabled(false);
	}

	public void setDados(Object obj){
		Valor vlr = (Valor) obj;
		if(vlr.getIs() < 10)
			tfIs.setText("00" + String.valueOf(vlr.getIs()));
		else if(vlr.getIs() < 10)
				tfIs.setText("0" + String.valueOf(vlr.getIs()));
			else
				tfIs.setText(String.valueOf(vlr.getIs()));
		tfAno.setText(String.valueOf(vlr.getAno()));
		tfCodTur.setText(vlr.getTurma().getCodigo());
		String valor = String.valueOf(vlr.getValor());
		valor = valor.replace(".", ",");
		if(valor.split(",")[0].length() == 1)
			valor = "0" + valor;
		if(valor.split(",")[1].length() == 1)
			valor = valor + "0";
		tfTVlr.setText("R$ " + valor);
	}
	
	public Valor getDados(){
		Valor vlr = new Valor();
		vlr.setIs(Integer.valueOf(tfIs.getText()));
		vlr.setAno(Integer.valueOf(tfAno.getText()));
		String valor;
		if(tpTipo.getSelectedIndex() > 0){
			vlr.setTurma(Turma.getTurma(tfCodAtv.getText()));
			valor = tfAVlr.getText().substring(2).trim();
		}else{
			vlr.setTurma(Turma.getTurma(tfCodTur.getText()));
			valor = tfTVlr.getText().substring(2).trim();
		}
		valor = valor.replace(",", ".");
		vlr.setValor(Float.valueOf(valor));
		vlr.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		vlr.setDtOpe(new Date(System.currentTimeMillis()));
		return vlr;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}
